package com.wulis.provider.authority.aspect;

import static com.wulis.common.constant.GlobalConstant.ROLE_MENU_BUTTON;

import javax.annotation.Resource;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * @author WuliBao
 * @date 2020-04-24
 */
@Slf4j
@Aspect
@Component
public class RoleChangeAop {
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    /**
     * 定义切点：@RoleChange标记方法
     */
    @Pointcut("@annotation(com.wulis.provider.authority.annotation.RoleChange)")
    public void roleChangeAspect() {
    }
    
    /**
     * 环绕通知,执行目标方法后，删除redis中的角色权限信息集合
     */
    @Around(value = "roleChangeAspect()")
    public Object aroundListener(ProceedingJoinPoint joinPoint) {
        // 执行业务方法（被切面方法）
        try {
            Object proceed = joinPoint.proceed(joinPoint.getArgs());
            // 删除角色权限信息
            redisTemplate.delete(ROLE_MENU_BUTTON);
            return proceed;
        }
        catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}
